package com.example.uiautomator;

import android.app.Instrumentation;
import android.os.RemoteException;
import android.view.KeyEvent;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.UiCollection;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiScrollable;
import androidx.test.uiautomator.UiSelector;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class TestUi {
    public Instrumentation mInstrumentation;
    public UiDevice mUiDevice;

    @Before
    public void setUp() {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mUiDevice = UiDevice.getInstance(mInstrumentation);
    }

    /**
     * 模拟物理键
     */
    @Test
    public void testHome() throws RemoteException {
//        mUiDevice.pressHome();
//        mUiDevice.c(2000);
//        mUiDevice.pressMenu();
//        mUiDevice.waitForIdle(2000);
//        mUiDevice.pressRecentApps();

//        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_H);
//        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_U);
//        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_A);
//        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_N);
//        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_G);
//

        //拖拽
//        mUiDevice.drag(900,2200,700,1000,50);
//         //等待时间
//        mUiDevice.waitForIdle(5000);
//
//        mUiDevice.drag(700,1000,900,2200,50);

        //滑动页面
//        mUiDevice.swipe(800,1500,200,1500,20);
//        mUiDevice.waitForIdle(5000);
//        mUiDevice.swipe(200,1500,800,1500,20);

        //下拉通知栏
//        mUiDevice.openNotification();
        //快速设置
        mUiDevice.openQuickSettings();
    }


    /**
     * UiSelector核心类
     *
     * @throws UiObjectNotFoundException
     */
    @Test
    public void testUiSelector() throws UiObjectNotFoundException {
        /*
        //text查找
        UiSelector uiSelector = new UiSelector().text("钱包");
        UiObject uiObject = new UiObject(uiSelector);
        uiObject.click();
        */

        /*
         //textContains (包含字符)查找
         UiSelector uiSelector = new UiSelector().textContains("包");
         UiObject uiObject = new UiObject(uiSelector);
         uiObject.click();
         */

        /*
         //textMatches (正则表达式)查找
         UiSelector uiSelector = new UiSelector().textMatches(".*W.*");
         UiObject uiObject = new UiObject(uiSelector);
         uiObject.click();
         */

        /*
        //textStartsWith (首字符)查找
        UiSelector uiSelector = new UiSelector().textStartsWith("电");
        UiObject uiObject = new UiObject(uiSelector);
        uiObject.click();
        */

        /*
        //description 查找——>(descriptionContains,descriptionMatches,descriptionStartsWith)
        UiSelector uiSelector = new UiSelector().description("浏览器");
        UiObject uiObject = new UiObject(uiSelector);
        uiObject.click();
        */

        /*
        //packageName 查找
        UiSelector uiSelector = new UiSelector().packageName("com.tencent.mm");
        UiObject uiObject = new UiObject(uiSelector);
        uiObject.click();
        */

        //className+index
        UiSelector uiSelector = new UiSelector().className("android.widget.TextView").index(1);
        UiObject uiObject = new UiObject(uiSelector);
        uiObject.click();

        //.......各种属性皆在 UiSelector 类中查看
    }

    /**
     * UiObject核心类
     *
     */
    @Test
    public void testUiObject() throws UiObjectNotFoundException {
        UiSelector uiSelector = new UiSelector().text("钱包");

        UiObject uiObject = new UiObject(uiSelector);
        //普通点击
//        uiObject.click();
        //点击后等在新的窗口打开
        uiObject.clickAndWaitForNewWindow();
        //点击后等在新的窗口打开（超时时间）
        uiObject.clickAndWaitForNewWindow(1000);
        //点击左上角
        uiObject.clickTopLeft();
        //长点击
        uiObject.longClick();

        //把当前按钮拖到某个图标上面
        uiObject.dragTo(new UiObject(new UiSelector().resourceId("com.huawei.android.launcher:id/workspace_screen")),20);
        //滑动（上下左右）
        uiObject.swipeUp(20);
    }

    /**
     * UiCollection核心类
     *  对象的集合进行操作
     */
    @Test
    public void testUiCollection() throws UiObjectNotFoundException {
        //找的是个父布局
        UiCollection viewGroup = new UiCollection(new UiSelector().className("android.widget.LinearLayout").index(3));
        //通过父布局找子控件
        UiObject div = viewGroup.getChildByDescription(new UiSelector().resourceId("com.android.calculator2:id/div"), "除");
        UiObject mul = viewGroup.getChildByDescription(new UiSelector().resourceId("com.android.calculator2:id/mul"), "乘");

        //找的是个父布局
        UiCollection viewGroup1 = new UiCollection(new UiSelector().className("android.widget.LinearLayout").index(5));
        //通过父布局找子控件
        UiObject digit7 = viewGroup1.getChildByText(new UiSelector().resourceId("com.android.calculator2:id/digit7"), "7");
        UiObject digit8 = viewGroup1.getChildByText(new UiSelector().resourceId("com.android.calculator2:id/digit8"), "8");
        UiObject digit9 = viewGroup1.getChildByText(new UiSelector().resourceId("com.android.calculator2:id/digit9"), "9");


        //找的是个父布局
        UiCollection viewGroup2 = new UiCollection(new UiSelector().className("android.widget.LinearLayout").index(9));
        //通过父布局找子控件
        UiObject equal = viewGroup2.getChildByDescription(new UiSelector().resourceId("com.android.calculator2:id/equal"), "等于");

        digit7.click();
        mul.click();
        digit8.click();
        digit9.click();
        equal.click();

        //一共多少子节点
        int childCount = viewGroup1.getChildCount();
        System.out.println("childCount==>"+childCount);//7

        //条件为button的子节点
        int childCount1 = viewGroup1.getChildCount(new UiSelector().className("android.widget.Button"));
        System.out.println("childCount1==>"+childCount1);//3
    }
    /**
     * UiScrollable核心类
     *  滚动的页面处理
     */
    @Test
    public void testUiScrollable() throws UiObjectNotFoundException {
        UiScrollable uiScrollable = new UiScrollable(new UiSelector().className("android.widget.ScrollView"));
        //向前滚动
//        uiScrollable.scrollForward(100);
        //向后滚动
//        uiScrollable.scrollBackward();
        //滚动到最前（次数）
//        uiScrollable.scrollToBeginning(5);
        //滚动到结尾（次数）
        uiScrollable.scrollToEnd(5);

        //快速的滚动（flingForward，flingBackward,flingToEnd,flingToBeginning）
        uiScrollable.flingToBeginning(3);

        //关于到某个位置
        uiScrollable.scrollTextIntoView("关于手机");

        //关于到某个位置
        uiScrollable.scrollIntoView(new UiSelector().text("关于手机"));

        //滚动找到对象并点击
        UiObject childByText = uiScrollable.getChildByText(new UiSelector().text("关于手机"), "关于手机", true);
        childByText.click();
    }
}
